package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.service.MarketInternalGoodsCodeService
import org.springframework.web.bind.annotation.*

@RequestMapping("markets/{marketId}/internalGoodsCodes")
@RestController
class MarketInternalGoodsCodeController(
    private val marketInternalGoodsCodeService: MarketInternalGoodsCodeService
) {

    // GET /markets/{marketId}/internalGoodsCodes
    @HasAuthority(Authority.卖场_内部码_查询)
    @GetMapping
    fun getAllInternalGoodsCodes(
        @PathVariable marketId: Int
    ): List<MarketInternalGoodsCodeResponse> {
        return marketInternalGoodsCodeService.findAllByMarketId(marketId)
    }

    // GET /markets/{marketId}/internalGoodsCodes/{id}
    @HasAuthority(Authority.卖场_内部码_查询)
    @GetMapping("{id}")
    fun getInternalGoodsCodeById(
        @PathVariable marketId: Int,
        @PathVariable id: Int
    ): MarketInternalGoodsCodeResponse {
        return marketInternalGoodsCodeService.findByIdAndMarketId(id, marketId)
    }

    // POST /markets/{marketId}/internalGoodsCodes
    @HasAuthority(Authority.卖场_内部码_创建)
    @PostMapping
    fun addInternalGoodsCode(
        @PathVariable marketId: Int,
        @RequestBody addRequest: AddMarketInternalGoodsCodeRequest
    ): MarketInternalGoodsCodeResponse {
        return marketInternalGoodsCodeService.addInternalGoodsCode(marketId, addRequest)
    }

    @HasAuthority(Authority.卖场_内部码_修改)
    // PUT /markets/{marketId}/internalGoodsCodes/{id}
    @PutMapping("{id}")
    fun updateInternalGoodsCode(
        @PathVariable marketId: Int,
        @PathVariable id: Int,
        @RequestBody updateRequest: UpdateMarketInternalGoodsCodeRequest
    ): MarketInternalGoodsCodeResponse {
        return marketInternalGoodsCodeService.updateInternalGoodsCode(id, marketId, updateRequest)
    }

    @HasAuthority(Authority.卖场_内部码_删除)
    // DELETE /markets/{marketId}/internalGoodsCodes/{id}
    @DeleteMapping("{id}")
    fun deleteInternalGoodsCode(
        @PathVariable marketId: Int,
        @PathVariable id: Int
    ) {
        marketInternalGoodsCodeService.deleteInternalGoodsCode(id, marketId)
    }

    // Request 和 Response 类定义在 Controller 内部
    data class AddMarketInternalGoodsCodeRequest(
        val code: String,
        val goodsId: Int
    )

    data class UpdateMarketInternalGoodsCodeRequest(
        val code: String
    )

    data class MarketInternalGoodsCodeResponse(
        val id: Int,
        val code: String,
        val goodsId:Int,
        val name: String,
        val barcode:String,
    )
}